home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Toolbox / MDEF.Sample / Sample.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  10.7 KB  |  318 lines  |  [TEXT/MPS ]

  1. {[n+,u+,r+,d+,#+,j=13-/40/1o,t=2,o=95] PasMat formatting options}
  2. {------------------------------------------------------------------------------
  3.  
  4. FILE Sample.p
  5.  Copyright Apple Computer, Inc. 1985-1987
  6.  All rights reserved.
  7.  
  8. NAME
  9.   Sample
  10.  
  11. DESCRIPTION
  12.   A small sample application written by Macintosh User Education.
  13.   It displays a single, fixed-size window in which the user can enter and edit
  14.   text.
  15.  
  16. ------------------------------------------------------------------------------}
  17. {$R-} { Turn off range checking}
  18. PROGRAM Sample;
  19.  {
  20.   The USES clause brings in the units containing the Pascal interfaces.
  21.  }
  22.  
  23.   USES Memtypes, Quickdraw, OSIntf, ToolIntf;
  24.  
  25.   PROCEDURE _DataInit;
  26.     EXTERNAL;
  27.  
  28.   CONST
  29.     appleID      = 128;                {resource IDs/menu IDs for Apple, File and Edit menus}
  30.     fileID         = 129;
  31.     editID         = 130;
  32.     styleID         = 11;
  33.     caseID         = 26;
  34.  
  35.     appleM         = 1;                   {index for each menu in myMenus (array of menu handles)}
  36.     fileM         = 2;
  37.     editM         = 3;
  38.  
  39.     menuCount     = 3;                   {total number of menus}
  40.  
  41.     windowID     = 128;                {resource ID for application's window}
  42.  
  43.     undoCommand  = 1;                   {menu item numbers identifying commands in Edit menu}
  44.     cutCommand     = 3;
  45.     copyCommand  = 4;
  46.     pasteCommand = 5;
  47.     clearCommand = 6;
  48.  
  49.     aboutMeCommand = 1;                {menu item in apple menu for About sample item}
  50.  
  51.   VAR
  52.     myMenus:     ARRAY [1..menuCount] OF MenuHandle; {array of handles to the menus}
  53.     dragRect:     Rect;                   {rectangle used to mark bounds for dragging window}
  54.     txRect:      Rect;                   {rectangle for text in application window}
  55.     textH:         TEHandle;               {handle to information about the text}
  56.     theChar:     CHAR;                   {character typed on the keyboard or keypad}
  57.     extended:     BOOLEAN;               {TRUE if user is Shift-clicking}
  58.     doneFlag:     BOOLEAN;               {TRUE if user has chosen Quit command}
  59.     myEvent:     EventRecord;           {information about an event}
  60.     wRecord:     WindowRecord;           {information about the application window}
  61.     myWindow:     WindowPtr;            {pointer to wRecord}
  62.     whichWindow: WindowPtr;            {ptr to window in which mouse button was pressed}
  63.     mousePt:     Point;                {point where cursor is located}
  64.     iBeamHdl:     CursHandle;           {Handle to the I-beam cursor image}
  65.     StyleMenu:     MenuHandle;
  66.     CaseMenu:     MenuHandle;
  67.     BMenu:         MenuHandle;
  68.     AMenu:        MenuHandle;
  69.     FMenu:        MenuHandle;
  70.     {[j=0] PasMat formatting option}
  71.  
  72.     {$S Initialize}                    {The menu initialization code lives in its own code
  73.                                         segment which can be unloaded once it's done with}
  74.  
  75.  
  76.  
  77.   PROCEDURE SetUpMenus;
  78.   { set up menus and menu bar }
  79.  
  80.     VAR
  81.       i: INTEGER;
  82.  
  83.     BEGIN
  84.   {Read menu descriptions from resource file into memory and store handles
  85.   in myMenus array}
  86.       myMenus[appleM] := GetMenu(appleID); {read Apple menu from resource file}
  87.       AddResMenu(myMenus[appleM], 'DRVR'); {add desk accessory names to Apple menu}
  88.       myMenus[fileM] := GetMenu(fileID); {read file menu from resource file}
  89.       myMenus[editM] := GetMenu(editID); {read Edit menu from resource file}
  90.  
  91.       FOR i := 1 TO menuCount DO InsertMenu(myMenus[i], 0); {install menus in menu bar}
  92.          
  93.         
  94.         StyleMenu := GetMenu(11);
  95.         CaseMenu := GetMenu(26);
  96.         AMenu := GetMenu(27);
  97.         BMenu := GetMenu(28);
  98.         FMenu := GetMenu(126);
  99.         InsertMenu(StyleMenu, 0);
  100.         InsertMenu(CaseMenu, -1);
  101.         InsertMenu(AMenu, -1);
  102.         InsertMenu(BMenu, -1);
  103.         InsertMenu(FMenu,-1);
  104.         
  105.  
  106.       DrawMenuBar;                       {and draw menu bar}
  107.     END; {of SetUpMenus}
  108.  
  109.   {$S Main}                            {the rest of the code belongs in the main segment}
  110.  
  111.   PROCEDURE ShowAboutMeDialog;
  112.   { Display a dialog box in response to the 'About Sample' menu item}
  113.  
  114.     CONST
  115.       aboutMeDLOG = 128;
  116.       okButton = 1;
  117.       authorItem = 2;
  118.       languageItem = 3;
  119.  
  120.     VAR
  121.       itemHit, itemType: INTEGER;
  122.       itemHdl: Handle;
  123.       itemRect: Rect;
  124.       theDialog: DialogPtr;
  125.  
  126.     BEGIN
  127.       theDialog := GetNewDialog(aboutMeDLOG, NIL, WindowPtr( - 1));
  128.       GetDitem(theDialog, authorItem, itemType, itemHdl, itemRect);
  129.       SetIText(itemHdl, 'Ming The Merciless');
  130.       GetDitem(theDialog, languageItem, itemType, itemHdl, itemRect);
  131.       SetIText(itemHdl, 'Pascal');
  132.  
  133.       REPEAT
  134.         ModalDialog(NIL, itemHit)
  135.       UNTIL (itemHit = okButton);
  136.  
  137.       CloseDialog(theDialog);
  138.     END; {of ShowAboutMeDialog}
  139.  
  140.   PROCEDURE DoCommand(mResult: LONGINT);
  141.   { Execute command specified by mResult, the result of MenuSelect }
  142.  
  143.     VAR
  144.       theItem: INTEGER;                {menu item number from mResult low-order word}
  145.       theMenu: INTEGER;                {menu number from mResult high-order word}
  146.       name: Str255;                    {desk accessory name}
  147.       temp: INTEGER;
  148.       templ: LONGINT;
  149.  
  150.     BEGIN
  151.       theItem := LoWord(mResult);       {call Toolbox Utility routines to}
  152.       theMenu := HiWord(mResult);       {set menu item number and menu}
  153.       {number}
  154.  
  155.       CASE theMenu OF                   {case on menu ID}
  156.  
  157.         appleID:
  158.           IF (theItem = aboutMeCommand) THEN
  159.             ShowAboutMeDialog
  160.           ELSE
  161.             BEGIN                       {call Menu Manager to get desk acc.}
  162.             GetItem(myMenus[appleM], theItem, name); {name, and call Desk Mgr. to open}
  163.             temp := OpenDeskAcc(name); {accessory (OpenDeskAcc result not used)}
  164.             SetPort(myWindow);           {call QuickDraw to restore applic.}
  165.             END; {of appleID}           {window as grafPort to draw in (may have been changed
  166.                                         during OpenDeskAcc)}
  167.         fileID: doneFlag := TRUE;       {quit (main loop repeats until doneFlag is true)}
  168.  
  169.         editID:
  170.           BEGIN                        {call Desk Manager to handle editing}
  171.           IF NOT SystemEdit(theItem - 1) {command if desk accessory window is the active
  172.                                           window}
  173.              THEN                       {applic. window is the active window}
  174.             CASE theItem OF            {case on menu item (command) number call TextEdit to
  175.                                         handle command}
  176.               cutCommand:
  177.                 BEGIN
  178.                 TECut(textH);
  179.                 templ := ZeroScrap;
  180.                 temp := TEtoScrap;
  181.                 END;
  182.               copyCommand:
  183.                 BEGIN
  184.                 TECopy(textH);
  185.                 templ := ZeroScrap;
  186.                 temp := TEtoScrap;
  187.                 END;
  188.               pasteCommand:
  189.                 BEGIN
  190.                 temp := TEfromScrap;
  191.                 TEPaste(textH);
  192.                 END;
  193.               clearCommand: TEDelete(textH);
  194.             END; {of item CASE}
  195.           END; {of editID}
  196.  
  197.       END; {of menu CASE}               {to indicate completion of command,}
  198.       HiliteMenu(0);                   {call Menu Manager to unhighlight menu title
  199.                                         (highlighted by MenuSelect)}
  200.     END; {of DoCommand}
  201.  
  202.   BEGIN                                {main program}
  203.     { Initialization }
  204.     UnLoadSeg(@_DataInit);               {remove data initialization code before any allocations}
  205.     InitGraf(@thePort);                {initialize QuickDraw}
  206.     InitFonts;                           {initialize Font Manager}
  207.     FlushEvents(everyEvent, 0);        {call OS Event Mgr to discard any previous events}
  208.     InitWindows;                       {initialize Window Manager}
  209.     InitMenus;                           {initialize Menu Manager}
  210.     TEInit;                            {initialize TextEdit}
  211.     InitDialogs(NIL);                   {initialize Dialog Manager}
  212.     InitCursor;                        {call QuickDraw to make cursor (pointer) an arrow}
  213.  
  214.     SetUpMenus;                        {set up menus and menu bar}
  215.     UnLoadSeg(@SetUpMenus);            {remove the once-only code}
  216.     WITH screenBits.bounds DO           {call QuickDraw to set dragging boundaries;}
  217.       SetRect(dragRect, 4, 24, right - 4, bottom - 4); {ensure at least 4 by 4 pixels will
  218.                                                         remain visible}
  219.     doneFlag := FALSE;                   {flag to detect when Quit command is chosen}
  220.  
  221.     myWindow := GetNewWindow(windowID, @wRecord, POINTER( - 1)); {put up applic. window}
  222.     SetPort(myWindow);                   {set current grafPort to this window}
  223.     txRect := thePort^.portRect;       {rectangle for text in window; InsetRect brings it}
  224.     InsetRect(txRect, 4, 0);           {in 4 pixels from left and right edges of window}
  225.     textH := TENew(txRect, txRect);    {call TextEdit to prepare for receiving text}
  226.  
  227.     iBeamHdl := GetCursor(iBeamCursor);
  228.  
  229.     { Main event loop }
  230.     REPEAT                               {call Desk Manager to perform any periodic}
  231.       SystemTask;                       {actions defined for desk accessories}
  232.  
  233.       IF (myWindow = frontWindow) THEN
  234.         BEGIN
  235.         GetMouse(mousePt);
  236.         IF PtInRect(mousePt, myWindow^.portRect) THEN
  237.           SetCursor(iBeamHdl^^)
  238.         ELSE
  239.           SetCursor(arrow);
  240.         TEIdle(textH);                   {call TextEdit to make vertical bar blink}
  241.         END;
  242.  
  243.       IF GetNextEvent(everyEvent, myEvent) THEN {call Toolbox Event Manager to get the next
  244.                                                  event that the application should handle}
  245.         CASE myEvent.what OF           {case on event type}
  246.  
  247.           mouseDown:                   {mouse button down: call Window Mgr to find out where}
  248.             CASE FindWindow(myEvent.where, whichWindow) OF
  249.  
  250.               inSysWindow:               {desk accessory window: call Desk Manager to handle it}
  251.                 SystemClick(myEvent, whichWindow);
  252.  
  253.               inMenuBar:               {menu bar: call Menu Manager to learn which command,
  254.                                         then execute it}
  255.                 DoCommand(MenuSelect(myEvent.where));
  256.  
  257.               inDrag:                   {title bar: call Window Manager to drag}
  258.                 DragWindow(whichWindow, myEvent.where, dragRect);
  259.  
  260.               inContent:               {body of application window: }
  261.                 BEGIN                   {call Window Manager to check whether it's the active
  262.                                         window}
  263.                 SysBeep(30);
  264.                 IF whichWindow <> frontWindow THEN
  265.                   SelectWindow(whichWindow) {and make it active if not}
  266.                 ELSE IF whichWindow = myWindow THEN
  267.                   BEGIN                {already active}
  268.                   GlobalToLocal(myEvent.where); {convert to window coordinates for TEClick}
  269.                   extended := BAnd(myEvent.modifiers, shiftKey) <> 0;
  270.                   TEClick(myEvent.where, extended, textH); {call TextEdit to process the event}
  271.                   END;
  272.                 END; {of inContent}
  273.             END; {of mouseDown}
  274.  
  275.           keyDown, autoKey:            {key pressed once or held down to repeat}
  276.             IF myWindow = frontWindow THEN
  277.               BEGIN
  278.               theChar := CHR(BAnd(myEvent.message, charCodeMask)); {get the char}
  279.               IF BAnd(myEvent.modifiers, cmdKey) <> 0 THEN {if Command key down, call Menu
  280.                                                             Manager to learn which}
  281.                 DoCommand(MenuKey(theChar))
  282.               ELSE
  283.                 TEKey(theChar, textH); {command, then execute it and pass char to TextEdit}
  284.               END; {of keyDown and autoKey}
  285.              
  286.               
  287.  
  288.           activateEvt:
  289.             IF WindowPtr(myEvent.message) = myWindow THEN
  290.               BEGIN
  291.               IF BAnd(myEvent.modifiers, activeFlag) <> 0 THEN {application window is becoming
  292.                                                                 active: call}
  293.                 BEGIN                   {TextEdit to highlight selection or display }
  294.                 TEActivate(textH);       {blinking vertical bar, and call Menu Mgr to}
  295.                 DisableItem(myMenus[editM], undoCommand); {disable Undo (since }
  296.                 END                    {application doesn't support Undo)}
  297.               ELSE
  298.                 BEGIN                   {application window is becoming inactive: }
  299.                 TEDeactivate(textH);   {unhighlight selection or remove blinking }
  300.                 EnableItem(myMenus[editM], undoCommand); {vertical bar, and enable Undo (since
  301.                                                           desk accessory may support it)}
  302.                 END;
  303.               END; {of activateEvt}
  304.  
  305.           updateEvt:                   {window appearance needs updating}
  306.             IF WindowPtr(myEvent.message) = myWindow THEN
  307.               BEGIN
  308.               BeginUpdate(WindowPtr(myEvent.message)); {call Window Mgr to begin update}
  309.               EraseRect(thePort^.portRect); {call QuickDraw to erase text area}
  310.               TEUpdate(thePort^.portRect, textH); {call TextEdit to update the text}
  311.               EndUpdate(WindowPtr(myEvent.message)); {call Window Mgr to end update}
  312.               END; {of updateEvt}
  313.  
  314.         END; {of event CASE}
  315.  
  316.     UNTIL doneFlag;
  317.   END.
  318.